package mapper

import (
	"context"
	"gorm.io/gorm"
	"mall/services/product/rpc/internal/domain/bo"
	"mall/services/product/rpc/internal/initialize/mysql"
	"mall/services/product/rpc/internal/mapper/entity"
)

var skuStockAllColumn = "stock_id, create_time, update_time, sku_id, actual_stock, lock_stock, stock)"

type SkuStockMapper struct {
}

func NewSkuStockMapper() *SkuStockMapper {
	return &SkuStockMapper{}
}

func (m *SkuStockMapper) SelectCountCtx(ctx context.Context, db *gorm.DB, skuStoreBo bo.SkuStoreBO) (totalCount int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SkuStockMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var total int64
	userEntity := entity.SpuSkuAttrValueEntity{}
	tx = tx.Select(skuStockAllColumn).Table(userEntity.TableName())
	m.getAllWhereSql(tx, skuStoreBo)
	tx = tx.Count(&total)
	return total, tx.Error
}
func (m *SkuStockMapper) SelectOneCtx(ctx context.Context, db *gorm.DB, skuStoreBo bo.SkuStoreBO) (skuStockEntity entity.SkuStockEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SkuStockMapper#SelectOneCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	storeEntity := entity.SkuStockEntity{}
	tx = tx.Select(skuStockAllColumn).Table(storeEntity.TableName())
	m.getAllWhereSql(tx, skuStoreBo)
	tx = tx.First(&storeEntity)
	return storeEntity, tx.Error
}
func (m *SkuStockMapper) SelectListCtx(ctx context.Context, db *gorm.DB, skuStoreBo bo.SkuStoreBO) (skuStockArr []entity.SkuStockEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SkuStockMapper#SelectListCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var storeEntityArr []entity.SkuStockEntity
	storeEntity := entity.SkuStockEntity{}
	tx = tx.Select(skuStockAllColumn).Table(storeEntity.TableName())
	m.getAllWhereSql(tx, skuStoreBo)
	tx = tx.Find(&storeEntityArr)
	return storeEntityArr, tx.Error
}

func (m *SkuStockMapper) getAllWhereSql(db *gorm.DB, storeBo bo.SkuStoreBO) {
	if storeBo.StockId > 0 {
		db.Where("stock_id = ?", storeBo.StockId)
	}
	if storeBo.SkuId > 0 {
		db.Where("sku_id = ?", storeBo.SkuId)
	}
	if storeBo.ActualStock > 0 {
		db.Where("actual_stock = ?", storeBo.ActualStock)
	}
	if storeBo.Stock > 0 {
		db.Where("stock = ?", storeBo.Stock)
	}
	if storeBo.LockStock > 0 {
		db.Where("lock_stock = ?", storeBo.LockStock)
	}
	if storeBo.StartTime != nil {
		db.Where("create_time = ?", storeBo.StartTime)
	}
	if storeBo.EndTime != nil {
		db.Where("create_time = ?", storeBo.EndTime)
	}
}

func (m *SkuStockMapper) BatchInsertCtx(ctx context.Context, engin *gorm.DB, arr []entity.SkuStockEntity) (idArr []int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SkuStockMapper#BatchInsertCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Create(&arr)
	var ids []int64
	for _, v := range arr {
		ids = append(idArr, v.SkuId)
	}
	return ids, tx.Error
}

func (m *SkuStockMapper) UpdateCtx(ctx context.Context, engin *gorm.DB, stockEntity *entity.SkuStockEntity) (err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SkuStockMapper#UpdateCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Where("stock_id = ?", stockEntity.StockId).Omit("stock_id").
		UpdateColumns(stockEntity)
	return tx.Error
}
